package com.quan.upms.web.controller;
import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.base.bean.PageBean;
import com.quan.common.base.bean.ResultBean;
import com.quan.common.core.support.BaseController;
import com.quan.common.utils.MenuTreeNode;
import com.quan.common.utils.MenuTreeUtil;
import com.quan.upms.entity.UpmsMenu;
import com.quan.upms.entity.UpmsRolePermission;
import com.quan.upms.service.UpmsMenuService;
import com.quan.upms.service.UpmsRolePermissionService;

import tk.mybatis.mapper.entity.Condition;

/**
 * <p>
 * 通用用户权限系统-菜单表 前端控制器
 * </p>
 *
 * @author 杨浩泉
 * @since 2018-05-13
 */
@RestController
@RequestMapping("/upms/menu")
public class UpmsMenuController extends BaseController {
	
	@Autowired 
	private UpmsMenuService service;

	@Autowired 
    private UpmsRolePermissionService rolePermissionService;
	
	/**
     * 添加
     * @param  entity 实体
     * @return ResultBean
     */
    @PostMapping("/add")
    public Object add(@Valid @RequestBody UpmsMenu entity, BindingResult bindingResult) {
    	if (bindingResult.hasErrors()) {
			return ResultBean.failure(bindingResult.getFieldError().getRejectedValue().toString());
		}
		UpmsMenu insertSelective = this.service.insertSelective(entity);
		if (null != insertSelective) {
			return ResultBean.ok();
		} else {
			return ResultBean.failure();
		}
    }

    /**
     * 删除
     * @param id ID
     * @return ResultBean
     */
    @DeleteMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id) {
    	
    	//删除角色权限中关联的菜单
		Condition condition = new Condition(UpmsRolePermission.class) ;
		condition.createCriteria().andEqualTo("menuId", id);
		this.rolePermissionService.delete(condition);
    	
    	int count = this.service.deleteById(id);
    	if(count > 0) {
    		return ResultBean.ok();
    	} else {
    		return ResultBean.failure("通过ID删除失败：[ " + id + " ]");
    	}
    }
    
    /**
     * 修改
     * @param  entity 实体
     * @return ResultBean
     */
    @PostMapping("/update")
    public Object update(@Valid @RequestBody UpmsMenu entity, BindingResult bindingResult) {
    	if (bindingResult.hasErrors()) {
            return ResultBean.failure(bindingResult.getFieldError().getDefaultMessage().toString()) ;
        }
    	int count = this.service.updateSelective(entity);
    	if(count > 0) {
    		return ResultBean.ok();
    	} else {
    		return ResultBean.failure();
    	}
    }

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return UpmsMenu
    */
    @GetMapping("/get/{id}")
    public Object get(@PathVariable Integer id) {
    	UpmsMenu entity = this.service.selectOneById(id);
    	if(null != entity) {
    		return ResultBean.ok(entity);
    	} else {
    		return ResultBean.failure("通过ID查询失败：[ " + id + " ]");
    	}
    }

   
    /**
     * 多条件查询数据列表
     *
     * @param entiy 实体对象（查询条件）
     * @return 分页对象
     */
     @GetMapping(value = "/list")
     public Object list(UpmsMenu entity) {
 		return ResultBean.ok(this.service.selectAll(entity));
     }

     /**
     * 多条件查询分页数据
     *
     * @param entiy 实体对象（查询条件）
     * @return 分页对象
     */
     @GetMapping(value = "/page")
     public Object page(UpmsMenu entity) {
 		return ResultBean.ok(this.service.selectPage(entity, new PageBean(request)));
     }
    
    /**
     * 树状JSON结构
     * @param entity
     * @return
     */
    @GetMapping(value = "/tree")
    public Object tree(UpmsMenu entity) {
    	Condition condition = new Condition(UpmsMenu.class);
    	condition.setOrderByClause("sort desc");
    	
    	List<UpmsMenu> menus = this.service.selectAllByCondition(condition);
    	
    	List<MenuTreeNode> treeNodes = new ArrayList<>();
    	for (UpmsMenu menu : menus) {
    		treeNodes.add(new MenuTreeNode(menu.getId(), menu.getPid(), menu.getName()));
    	}
    	
    	return new MenuTreeUtil().treeMenu(treeNodes);
    }
    
}
